package com.hcbd.tdwt.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;

import androidx.core.content.ContextCompat;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.model.GradientColor;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.pangu.bdsdk2021.entity.terminalthree.BDSnrInfo;

import java.util.ArrayList;
import java.util.List;

public class ChartUtil {
    private List<BarEntry> chartData;
    private List<BarEntry>twoChartData;
    private BarChart mBarChart;
    private Context mContext;
    private int topColor,bottomColor;
    private int xTvColor,xGridColor;
    private int leftTextColor;
    private int width=0;
    public ChartUtil(Context context,BarChart barChart){
        chartData=new ArrayList<>();
        twoChartData=new ArrayList<>();
        this.mBarChart=barChart;
        this.mContext=context;
    }
    public void initChartData(){
        chartData.add(new BarEntry(0, BDSnrInfo.obj.s1));
        chartData.add(new BarEntry(1, BDSnrInfo.obj.s2));
        chartData.add(new BarEntry(2, BDSnrInfo.obj.s3));
        chartData.add(new BarEntry(3, BDSnrInfo.obj.s4));
        chartData.add(new BarEntry(4, BDSnrInfo.obj.s5));
        chartData.add(new BarEntry(5, BDSnrInfo.obj.s6));
        chartData.add(new BarEntry(6, BDSnrInfo.obj.s7));
        chartData.add(new BarEntry(7, BDSnrInfo.obj.s8));
        chartData.add(new BarEntry(8, BDSnrInfo.obj.s9));
        chartData.add(new BarEntry(9, BDSnrInfo.obj.s10));
        twoChartData.add(new BarEntry(10, BDSnrInfo.obj.s11));
        twoChartData.add(new BarEntry(11, BDSnrInfo.obj.s12));
        twoChartData.add(new BarEntry(12, BDSnrInfo.obj.s13));
        twoChartData.add(new BarEntry(13, BDSnrInfo.obj.s14));
        twoChartData.add(new BarEntry(14, BDSnrInfo.obj.s15));
        twoChartData.add(new BarEntry(15, BDSnrInfo.obj.s16));
        twoChartData.add(new BarEntry(16, BDSnrInfo.obj.s17));
        twoChartData.add(new BarEntry(17, BDSnrInfo.obj.s18));
        twoChartData.add(new BarEntry(18, BDSnrInfo.obj.s19));
        twoChartData.add(new BarEntry(19, BDSnrInfo.obj.s20));
        twoChartData.add(new BarEntry(20, BDSnrInfo.obj.s21));
        chartData.addAll(twoChartData);
        DisplayMetrics dm =mContext.getResources().getDisplayMetrics();
        width = dm.widthPixels;
        setWidth(width+width/2);
        mBarChart.setTouchEnabled(false);
    }
    //更新图表
    public void updateChart(){
        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(BeidouBoxParams.horizontalAxis);
        YAxis leftAxis = mBarChart.getAxisLeft();
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(BeidouBoxParams.verticalAxis);
    }
    //初始化图表
    public void initChart() {
        //设置表格上的点，被点击的时候，的回调函数
        mBarChart.setDrawBarShadow(false);
        mBarChart.setDrawValueAboveBar(true);
        mBarChart.getDescription().setEnabled(false);
        mBarChart.setPinchZoom(false);
        mBarChart.setDrawGridBackground(false);
        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGridColor(mContext.getColor(xGridColor));
        xAxis.setTextColor(mContext.getColor(xTvColor));
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(BeidouBoxParams.horizontalAxis);
        mBarChart.getAxisRight().setEnabled(false);
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return "C"+(int) (value + 1);
            }
        });
        YAxis leftAxis = mBarChart.getAxisLeft();
        leftAxis.setLabelCount(4, false);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setTextColor(mContext.getColor(leftTextColor));
        leftAxis.setAxisMaximum(BeidouBoxParams.verticalAxis);
        mBarChart.setTouchEnabled(false);
        initChartData();
    }
    //更新图表数据
    public void updateChartData(){
        setXY(0,0,BDSnrInfo.obj.s1);
        setXY(1,1,BDSnrInfo.obj.s2);
        setXY(2,2,BDSnrInfo.obj.s3);
        setXY(3,3,BDSnrInfo.obj.s4);
        setXY(4,4,BDSnrInfo.obj.s5);
        setXY(5,5,BDSnrInfo.obj.s6);
        setXY(6,6,BDSnrInfo.obj.s7);
        setXY(7,7,BDSnrInfo.obj.s8);
        setXY(8,8,BDSnrInfo.obj.s9);
        setXY(9,9,BDSnrInfo.obj.s10);
        /*if(BeidouBoxParams.card_model==3) {
            if(chartData.size()<11){
                chartData.addAll(twoChartData);
                setWidth(width+width/2);
            }*/
            setXY(10,10,BDSnrInfo.obj.s11);
            setXY(11,11,BDSnrInfo.obj.s12);
            setXY(12,12,BDSnrInfo.obj.s13);
            setXY(13,13,BDSnrInfo.obj.s14);
            setXY(14,14,BDSnrInfo.obj.s15);
            setXY(15,15,BDSnrInfo.obj.s16);
            setXY(16,16,BDSnrInfo.obj.s17);
            setXY(17,17,BDSnrInfo.obj.s18);
            setXY(18,18,BDSnrInfo.obj.s19);
            setXY(19,19,BDSnrInfo.obj.s20);
            setXY(20,20,BDSnrInfo.obj.s21);
        /*}else{
            if(chartData.size()>10) {
                chartData.removeAll(twoChartData);
                setWidth(width);
            }
        }*/
        updateChart();
        setData(chartData);
    }
    public void setXY(int index,int x,int y){
        chartData.get(index).setX(x);
        chartData.get(index).setY(y);
    }
    //设置数据
    private void setData(List<BarEntry> list) {
        BarDataSet set1;
        if (mBarChart.getData() != null && mBarChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mBarChart.getData().getDataSetByIndex(0);
            set1.setValues(list);
            mBarChart.getData().notifyDataChanged();
        } else {
            set1 = new BarDataSet(list,"");
      /*      int bottomColor = ContextCompat.getColor(mContext,R.color.bright_green );
            //顶部渐变色
            int topColor = ContextCompat.getColor(mContext,R.color.light_green);*/
            //2.    新建List存储渐变色
            List<GradientColor> gradientColors = new ArrayList<>();
            gradientColors.add(new GradientColor(bottomColor, topColor));

            //3.    添加颜色List到BarDataSet
            //set类型是BarDataSet
            set1.setGradientColors(gradientColors);
            set1.setValueTextColor(mContext.getColor(xTvColor));
            set1.setValueFormatter(new ValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
                    return String.valueOf((int)value);
                }
            });
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(0.4f);
            //设置数据
            mBarChart.setData(data);
        }
        mBarChart.invalidate();
    }
    //设置图表宽度
    private void setWidth(int width){
        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) mBarChart.getLayoutParams();//获取当前控件的布局对象
        params.width=width;//设置当前控件布局的高度
        mBarChart.setLayoutParams(params);//将设置好的布局参数应用到控件中
    }

    public void setBottomColor(int bottomColor) {
        this.bottomColor = ContextCompat.getColor(mContext,bottomColor);
    }

    public void setTopColor(int topColor) {
        this.topColor = ContextCompat.getColor(mContext,topColor);
    }

    public void setxTvColor(int xTvColor) {
        this.xTvColor = xTvColor;
    }

    public void setLeftTextColor(int leftTextColor) {
        this.leftTextColor = leftTextColor;
    }

    public void setxGridColor(int xGridColor) {
        this.xGridColor = xGridColor;
    }
}
